﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YourPersonalTrainer.BusinessLayer;
using WMPLib;

namespace YourPersonalTrainer.ViewLayer
{
    public partial class WorkoutProgressUC : Form
    {
        private Workout m_Workout;
        private string m_CurrentItemName;
        private int m_SecondsRemaining;
        public WorkoutProgressUC()
        {
            InitializeComponent();
            axWindowsMediaPlayer1.settings.volume = 15;
            //axWindowsMediaPlayer1.URL = "d:\\test.wma";
            
        }

        public void Initialise(Workout workout)
        {
            m_Workout = workout;
            m_Workout.UpdateHandler += UpdateProgress;
            m_Workout.PlayAudioHandler = PlayAudio;
        }

        public void PlayAudio(string AudioFileLocation)
        {
            axWindowsMediaPlayer1.URL = AudioFileLocation;
        }

        private void timeRemainingLabel_Click(object sender, EventArgs e)
        {

        }

        public void UpdateProgress(object sender, string currentItemName, int secondsRemaining)
        {
            m_CurrentItemName = currentItemName;
            m_SecondsRemaining = secondsRemaining;
            MethodInvoker mi = new MethodInvoker(UpdateLabels);
            this.BeginInvoke(mi);
        }

        public void UpdateLabels()
        {
            workoutItemNameLabel.Text = m_CurrentItemName;
            timeRemainingLabel.Text = m_SecondsRemaining.ToString() + "s";
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            if (m_Workout != null)
            {
                m_Workout.Stop();
            }
            this.Hide();
        }

        private void skipButton_Click(object sender, EventArgs e)
        {
            m_Workout.Skip();
        }

        private void WorkoutProgressUC_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_Workout.UpdateHandler -= UpdateProgress; 
            if (m_Workout != null)
            {
                m_Workout.Stop();
            }
        }

        private void pauseButton_Click(object sender, EventArgs e)
        {
            m_Workout.Pause();
        }
    }
}
